/**
 * Created by zhai shuai shuai on 2017/6/27.
 */
var formatter = function (value, row, index) {
    return ['<a href="javascript:void(0)" class="detail">查看</a>'
    + '&nbsp;&nbsp;<a href="javascript:void(0)" class="deletepro">删除</a>'].join('');
}

bindClick("#clickVideo", function () {

    var filePath = $("input[name=videoPath]").val();

    if ("" != filePath) {
        var a = document.createElement("a");
        a.href = filePath;
        a.target = "_blank";
        a.click();
    } else {
        hint("未上传商业计划书")
    }

    return false;
});

bindClick("#okBtn", function () {
    var reason = $.trim($("#auditreason").val());
    var id = $("#id").val();
    var mobile = $("#mobile").val();
    comment.showOperationTips("确认审核通过申报项目吗？", function () {
        $.post($().AbsUrlParse('/web/userproject/auditproject'), {
            id: id,
            mobile: mobile,
            state: '审核通过',
            reason: reason
        }, function (data) {
            if (data.state == true) {
                if (data.res.code == 40000) {
                    closeModel("finddetail-div");
                    hint("申报项目已审核成功");
                    var table = $('#table');
                    table.bootstrapTable('refresh');
                } else {
                    hint(data.res.msg);
                }
            } else {
                hint(data.msg);
            }
        });
    });
    return false;
});

bindClick("#noBtn", function () {
    var reason = $.trim($("#auditreason").val());
    if ("" == reason) {
        hint("请填写审核失败原因");
        return false;
    }
    var id = $("#id").val();
    var mobile = $("#mobile").val();
    comment.showOperationTips("确认审核不通过申报项目吗？", function () {
        $.post($().AbsUrlParse('/web/userproject/auditproject'), {
            id: id,
            mobile: mobile,
            state: '审核失败',
            reason: reason
        }, function (data) {
            if (data.state == true) {
                if (data.res.code == 40000) {
                    closeModel("finddetail-div");

                    hint("申报项目已更改为审核失败");
                    var table = $('#table');
                    table.bootstrapTable('refresh');
                } else {
                    hint(data.res.msg);
                }
            } else {
                hint(data.msg);
            }
        });
    });
    return false;
});

//修改的代码
bindClick("#btnsearch", function () {

    //下面是每次查询
    var hangye = $("select[name=hangye]").val();
    var ipt_regtime = $("input[name=ipt_regtime]").val();
    var ipt_created = $("input[name=ipt_created]").val();
    var search = $("input[name=shengqingren]").val();

    //如果是组委的时候才会有这个
    var parkId = $("select[name=park_name]").val();

    //如果不是级委会的话，查询的时候每交都会带这两个参数过去
    var default_user_role = $("input[name=default_user_role]").val();
    var default_park_name = $("input[name=default_park_name]").val();

    var cstart = "";
    var cend = "";
    var start = "";
    var end = "";

    if (ipt_created != null) {
        cstart = ipt_created.split(" - ")[0];
        cend = ipt_created.split(" - ")[1];
    }

    if (ipt_regtime != null) {
        start = ipt_regtime.split(" - ")[0];
        end = ipt_regtime.split(" - ")[1];
    }

    var url = $().AbsUrlParse("/web/userproject/queryprojectsNameList?search=" + search
        + "&hangye=" + hangye + "&entrestartdate="
        + start + "&entreenddate=" + end + "&cstart=" + cstart + "&cend=" + cend + "&park_name=" + parkId + "" +
        "&default_user_role=" + default_user_role + "&default_park_name=" + default_park_name);

    $("#table").bootstrapTable("refresh", {"url": url});
});

//下面是初赛的导出
bindClick("#btnexport", function () {

    var hangye = $("select[name=hangye]").val();
    var search = $("input[name=shengqingren]").val();
    //如果是组委会管理员才会用到这个parkId
    var selectParkId = $("#yuanqu").val();
    var state = "审核通过";
    var ipt_regtime = $("input[name=ipt_regtime]").val();
    var ipt_created = $("input[name=ipt_created]").val();

    //得到userRole与得到parkName
    var userRole = $("#userRole").val();
    var dLuParkId = $("#parkName").val();

    var start = "", end = "";
    var cstart = "", cend = "";


    //项目的提交时间
    if (ipt_created != '') {
        cstart = ipt_created.split(" - ")[0];
        cend = ipt_created.split(" - ")[1];
    }
    //企业的注册时间
    if (ipt_regtime != '') {
        start = ipt_regtime.split(" - ")[0];
        end = ipt_regtime.split(" - ")[1];
    }

    if(userRole==undefined){
        userRole='';
    }
    if(dLuParkId==undefined){
        dLuParkId ='';
    }

    $.post($().AbsUrlParse("/web/userproject/exportNameList"), {
        search: search,
        hangye: hangye,
        selectParkId: selectParkId,
        state: state,
        entrestartdate: start,
        entreenddate: end,
        cstart: cstart,
        cend: cend,
        userRole: userRole,
        dLuParkId:dLuParkId
    }, function (data) {
        if (data.state == true) {
            if (data.res.code == 40000) {
                hint("导出成功");
                var a = document.createElement("a");
                a.href = $().AbsUrlParse("/excel/" + data.res.data);
                a.click();
            } else {
                hint(data.res.msg);
            }
        } else {
            hint(data.msg);
        }
    });

});


var auditFormatter = function (value, row, index) {
    if (value == '等待审核') {
        return ['<span style="color: blue">' + value + '</span>'].join('');
    } else if (value == '审核通过') {
        return ['<span style="color: lightgreen">' + value + '</span>'].join('');
    } else if (value == '审核失败') {
        return ['<span style="color:red">' + value + '</span>'].join('');
    }
}

function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}


//点击查看项目详情
var clickDetail = function (row) {
    showModel("finddetail-div", function () {
        if (row.auditState == '等待审核') {
            $("#okBtn").show();
            $("#noBtn").show();
        }
        if (row.videoPath != null && row.videoPath != '') {
            $("#clickVideo").show();
        } else {
            $("#clickVideo").hide();
        }
        setRowToFrm("detailProForm", row);
    });

    var filePath = $("input[name=attachmentPath]").val();
    if (filePath.indexOf(",") != -1) {
        var pathArray = filePath.split(",");
        var btnButton = "";
        for (var i = 0; i < pathArray.length; i++) {
            var filenameArray= pathArray[i].split('\/');
            var filename = filenameArray[filenameArray.length-1];
            btnButton = btnButton + "<button class='fileBtn' data-value='" + pathArray[i] + "'>" + (i + 1) + '. ' + filename +" - 点击下载</button><br/><br/>";
        }
        $("#downloadDiv").html(btnButton);
    } else {
        var filenameArray= row.attachmentPath.split('\/');
        var filename = filenameArray[filenameArray.length-1];
        $("#downloadDiv").html("<button class='fileBtn' data-value='" + row.attachmentPath + "'>" + filename +" - 点击下载</button>");
    }


    $("#downloadDiv").find(".fileBtn").click(function () {
        var filePath = $(this).data("value");
        if ("" != filePath) {
            var a = document.createElement("a");
            a.href = File_Download_Url + filePath;
            var filePathArray = filePath.split('\/');
            var filename = filePathArray[filePathArray.length - 1];
            a.download = filename;
            a.target = "_blank";
            a.click();
        } else {
            hint("未上传商业计划书");
        }
        return false;
    });
}

window.operateEvents = {
    'click .detail': function (e, value, row, index) {

        clickDetail(row);
    },

    'click .deletepro': function (e, value, row, index) {
        comment.showOperationTips("确定删除项目吗？", function () {
            $.post($().AbsUrlParse('/web/userproject/deleteproject'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $('#table');
                        table.bootstrapTable('refresh');
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};


//组委会给初赛发送邮件及短信
bindClick("#senddxyj",function(){
    comment.showOperationTips("确定发送邮件与短信吗？",function(){
        $.post($().AbsUrlParse("/web/userproject/sendYjDx"),{},function(data){

        });
    });
});



//初始化页面的时候运行该函数
$(function () {
    initDateRange();
});